package com.szy.weibo;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.szy.weibo.service.Weibo;
import com.szy.weibo.util.TextUtil;

public class MainActivity extends Activity
{
	private static final String TAG = "MainActivity";
	private final static int REQUEST_CODE=1;
	
	private Button btnAuthorization;
	SharedPreferences pres;
	
	private String requestToken;
	private String requestTokenSecret;
	private String verifier;
	private String accessToken;
	private String accessTokenSecret;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btnAuthorization=(Button)findViewById(R.id.btnAuthorization);
		btnAuthorization.setOnClickListener(listener);
		//从配置文件中读取accessToken和accessTokenSecret
		pres = this.getSharedPreferences("AccessToken", Context.MODE_PRIVATE);
		accessToken = pres.getString("accessToken", "");
		accessTokenSecret = pres.getString("accessTokenSecret", "");
		//当accessToken和accessTokenSecret都不为空时
		if (!TextUtil.isEmpty(accessToken)&&!TextUtil.isEmpty(accessTokenSecret))
		{
			Intent intent = new Intent(this, WeiBoActivity.class);
			intent.putExtra("accessToken", accessToken);
			intent.putExtra("accessTokenSecret", accessTokenSecret);
			startActivity(intent);
		}
	}
	
	private OnClickListener listener=new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			switch (v.getId())
			{
			case R.id.btnAuthorization:
				authorization();
				break;
			}
		}
	};

	/**
	 * 完成OAuth认证
	 */
	protected void authorization()
	{
		startWebView();
	}

	/**
	 * 启动WebView进行OAuth认证
	 */
	protected void startWebView()
	{
		String urlStr = "https://open.t.qq.com/cgi-bin/authorize";
		Weibo weibo = new Weibo();
		//获取未授权的Request Token
		Map<String, String> map = weibo.getRequestToken();
		//获取oauth_token
		requestToken = map.get("oauth_token");
		requestTokenSecret = map.get("oauth_token_secret");
		Log.i(TAG, "Request Token="+requestToken);
		Log.i(TAG, "Request Token Secret="+requestTokenSecret);
		//构造请求的URL
		StringBuilder urlBuilder = new StringBuilder();
		urlBuilder.append(urlStr);
		urlBuilder.append("?");
		urlBuilder.append("oauth_token="+requestToken);
		Intent intent = new Intent(MainActivity.this,WebViewActivity.class);
		Bundle bundle=new Bundle();
		bundle.putString("urlStr", urlBuilder.toString());
		intent.putExtras(bundle);
		startActivityForResult(intent, REQUEST_CODE);
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode==REQUEST_CODE)
		{
			if (resultCode==WebViewActivity.RESULT_CODE)
			{
				Bundle bundle=data.getExtras();
				//获取验证码
				verifier=bundle.getString("verifier");
				if (!TextUtil.isEmpty(verifier))
				{
					getAccessToken();
				}
			}
		}
	}
	
	protected void getAccessToken()
	{
		Weibo weibo = new Weibo();
		Map<String, String> map = weibo.getAccessToken(requestToken,requestTokenSecret, verifier);
		accessToken = map.get("oauth_token");
		accessTokenSecret=map.get("oauth_token_secret");
		Log.i(TAG, "Access Token="+accessToken);
		Log.i(TAG, "Access Token Secret="+accessTokenSecret);
		if (!TextUtil.isEmpty(accessToken))
		{
			Editor editor = pres.edit();
			editor.putString("accessToken", accessToken);
			editor.putString("accessTokenSecret", accessTokenSecret);
			editor.commit();
			
			Intent intent = new Intent(this, WeiBoActivity.class);
			intent.putExtra("accessToken", accessToken);
			intent.putExtra("accessTokenSecret", accessTokenSecret);
			startActivity(intent);
		}
	}
}